/***************************************************************************
 *  Filename: parsaxcmd.cpp
 *  Class: ParsaxCmd
 *
 *  Copyright 2012  Xander van Kooten
 *  <xandervk@gmail.com>
 *
 *  Copyright 2012  Mario Henrique Voorsluys
 *  <forcaeluz@gmail.com>
 *
 ****************************************************************************/

/*
 *
 * This file is part of Parsax-Remote-Controller.
 *
 *    Parsax-Remote-Controller is free software: you can redistribute it
 *    and/or modify it under the terms of the GNU General Public License
 *    as published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    Parsax-Remote-Controller is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Parsax-Remote-Controller.
 *    If not, see <http://www.gnu.org/licenses/>.
 */

#include "parsaxcmd.h"

ParsaxCmd::ParsaxCmd(QObject *parent) :
    QObject(parent), output(stdout), input(stdin)
{
    parser = new MessageParser(this);
    communicationHandler = new MasterCommunicator(this);
}

ParsaxCmd::~ParsaxCmd(){

}

void ParsaxCmd::loadSettings(){
    QSettings settings("CCC-Buienradar", "Parsax CMD_Master");
    settings.beginGroup("Network");
    slaveip = settings.value("slaveip", "127.0.0.1").toString();
    slaveport = settings.value("slaveport", 33300).toInt();
    settings.endGroup();
}

void ParsaxCmd::start(){

    output << tr("Please chose one of the options and press <Return>") << endl;
    output << tr(" A - Show local settings")<< endl;
    output << tr(" B - Change a local setting")<< endl;
    output << tr(" C - Show slave settings")<< endl;
    output << tr(" D - Change slave settings")<< endl;
    output << tr(" E - Start a program on slave")<< endl;
    output << tr(" F - Stop a program on slave")<< endl;
    output << tr(" G - Exit")<< endl;
    output << tr(" N - Connect")<< endl;
    in = input.readLine();
    if(!in.compare("A", Qt::CaseInsensitive)){
        showSettings();
    }else if(!in.compare("B", Qt::CaseInsensitive)){
        changeSettings();
    }else if(!in.compare("C", Qt::CaseInsensitive)){
        showRemoteSettings();
    }else if(!in.compare("D", Qt::CaseInsensitive)){
        changeRemoteSettings();
    }else if(!in.compare("E", Qt::CaseInsensitive)){
        startRemoteProgram();
    }else if(!in.compare("F", Qt::CaseInsensitive)){
        stopRemoteProgram();
    }else if(!in.compare("G", Qt::CaseInsensitive)){
        output << "Closing..." << endl;
        emit closeRequest();
    }else if(!in.compare("N", Qt::CaseInsensitive)){
        communicationHandler->connectToSlave(slaveip, slaveport);
    }
}

void ParsaxCmd::showSettings(){
    QSettings settings("CCC-Buienradar", "Parsax CMD_Master");
    QStringList settingKeys = settings.allKeys();
    output << "Currently there are " << settingKeys.size() << " settings saved on file" << endl;
    if(settingKeys.size()>0){
        output << "Settings are:" << endl;
        foreach(QString str, settingKeys){
            output << str << "\t\t\t" << settings.value(str).toString() << endl;
        }
    }
    output << "Press <Return> to go back to main loop" << endl;
    input.readLine();
}

void ParsaxCmd::changeSettings(){
    QSettings settings("CCC-Buienradar", "Parsax CMD_Master");
    QStringList settingKeys = settings.allKeys();
    output << "Currently there are " << settingKeys.size() << " settings saved on file" << endl;
    if(settingKeys.size()>0){
        output << "Settings are:" << endl;
        foreach(QString str, settingKeys){
            output << str << "\t\t" << settings.value(str).toString() << endl;
        }
    }
    output << "Type setting key(Empty key will exit) and press <RETURN>" << endl;
    QString key = input.readLine();
    if(key.isEmpty())
        return;
    output << "Type settings value and press <RETURN>" << endl;
    QString value = input.readLine();
    settings.setValue(key, value);
    output << "Setting changed";
}

void ParsaxCmd::showRemoteSettings(){
    /*!
        \todo Implement. This is not really necessary,
        as this is just a demo of how programs are started and stopped.
        */
}

void ParsaxCmd::changeRemoteSettings(){
    /*!
        \todo Implement. This is not really necessary,
        as this is just a demo of how programs are started and stopped.
        */
}

void ParsaxCmd::startRemoteProgram(){
    output << "Type of start:" << endl;
    output << " A -> New program" << endl;
    output << " B -> Saved program" << endl;
    QString in = input.readLine();
    if(!in.compare("A", Qt::CaseInsensitive)){
        output << "Chose a program id:" << endl;
        QString id = input.readLine();
        output << "Command line:" << endl;
        QString program = input.readLine();
        output << "Start arguments, separated by <space>:" << endl;
        QString param = input.readLine();
        QStringList params = param.split(" ", QString::SkipEmptyParts);
        QByteArray msg = parser->nprMessage(id, program, params);
        communicationHandler->sendData(msg);
    }else if(!in.compare("B", Qt::CaseInsensitive)){
        output << "Chose a program id:" << endl;
        QString id = input.readLine();
        QByteArray msg = parser->prgMessage(id, "start");
        communicationHandler->sendData(msg);
    }
}
void ParsaxCmd::stopRemoteProgram(){
    output << "Chose a program id:" << endl;
    QString id = input.readLine();
    QByteArray msg = parser->prgMessage(id, "stop");
    communicationHandler->sendData(msg);
}
